<!-- 协议 -->
<template>
    <div >
        <a-form :label-col="{ span: 7 }" :wrapper-col="{ span: 17 }" style="max-width: 900px">
            <a-form-item :label="item.name" v-for="item in agreementList" :key="item.agreementKey">
                <a-input v-model="item.agreementContext" allow-clear :placeholder="`请输入${item.name}`">
                    <a slot="addonAfter" href="javascript:void(0)" @click="showUrl(item.agreementContext)">查看</a>
                </a-input>
            </a-form-item>
            <a-form-item v-bind="formItemLayoutWithOutLabel">
                <a-button type="primary" @click="submit">保存</a-button>
            </a-form-item>
        </a-form>
    </div>
</template>

<script>
import { getAgreement , updateAgreement } from '@/api/system';
export default {
    data () {
        return {
            formItemLayoutWithOutLabel: {
                wrapperCol: {
                    xs: { span: 24, offset: 0 },
                    sm: { span: 24, offset: 16 },
                },
            },
            agreementList: [
                { 'agreementKey': 'USER_SERVICE', 'name': '用户服务协议' , 'agreementContext': ''},
                { 'agreementKey': 'USER_INFO', 'name': '个人信息保护政策' , 'agreementContext': ''},
                { 'agreementKey': 'USER_PRIVACY', 'name': '隐私政策' , 'agreementContext': ''},
            ],
        };
    },
    created(){
        this.getData();
    },
    methods: {
        // 获取协议内容
        getData(){
            let _this = this;
            getAgreement()
            .then(res => {
                if(res.success){
                    res.data.forEach(item => {
                        _this.agreementList.forEach(agr => {
                            if(item.agreementKey == agr.agreementKey){
                                agr.agreementContext = item.agreementContext;
                            }
                        })
                    })
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        // 保存内容
        submit(){
            let _this = this;
            for(let i = 0; i < _this.agreementList.length; i++){
                if(_this.agreementList[i].agreementContext != ''){
                    const params = {
                        'agreementKey': _this.agreementList[i].agreementKey,
                        'agreementContext': _this.agreementList[i].agreementContext,
                    }
                    updateAgreement(params)
                    .then(res => {
                        if(res.success){
                            if(i == _this.agreementList.length - 1){
                                _this.$notification['success']({
                                    message: '温馨提示',
                                    description: '修改成功',
                                    duration: 8
                                })
                            }
                        }else{
                            _this.$notification['error']({
                                message: '温馨提示',
                                description: res.errMessage,
                                duration: 8
                            })
                        }
                    })
                }
            }
        },
        // 查看协议
        showUrl(url){
            if(url && url != ''){
                window.open(url)
            }
        },
    }
}

</script>
<style scoped>

</style>